package com.qk.management.scheduled;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.qk.common.constant.CacheConstants;
import com.qk.domain.Business.BusinessPortalDO;
import com.qk.domain.clue.CluePortalDO;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.ClueMapper;
import com.qk.vo.portal.OverviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 缓存定时刷新任务
 */
@Slf4j
@Component
@SuppressWarnings("all")
public class CacheRefreshSchedule {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    
    // 定义缓存键前缀和后缀常量
    private static final String CACHE_PORTAL_KEY_SUFFIX = "getOverview";

    @Scheduled(cron = "0 0 0/1 * * ?")
    public void scheduledRefreshOverviewCache() {
        log.info("开始执行定时刷新概览数据缓存任务");
        CluePortalDO clueOverviewVO = clueMapper.getClueOverviewData();
        BusinessPortalDO businessOverviewVO = businessMapper.getBusinessOverviewData();
        OverviewVO overviewVO = BeanUtil.copyProperties(businessOverviewVO, OverviewVO.class);
        BeanUtil.copyProperties(clueOverviewVO, overviewVO);
        redisTemplate.opsForValue().set(CacheConstants.REDIS_CACHE_PREFIX + CACHE_PORTAL_KEY_SUFFIX, JSONUtil.toJsonStr(overviewVO));
        log.info("定时刷新概览数据缓存任务执行完毕，概览数据：{}", overviewVO);
    }
}
